package com.ruoyi.module.course.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.module.course.vo.CourseVO;
import com.ruoyi.module.courseDetail.domain.CourseDetail;
import com.ruoyi.module.courseDetail.service.ICourseDetailService;
import com.ruoyi.module.excellentCase.domain.ExcellentCase;
import com.ruoyi.module.excellentCase.vo.ExcellentCaseVO;
import com.ruoyi.module.excellentCaseDetail.domain.ExcellentCaseDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.module.course.domain.Course;
import com.ruoyi.module.course.service.ICourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author yqq
 * @date 2024-05-06
 */
@RestController
@RequestMapping("/api/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;

    @Autowired
    private ICourseDetailService iCourseDetailService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('api:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('api:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        Course course = courseService.selectCourseByCourseId(courseId);
        if (Objects.isNull(course)){
            return AjaxResult.error("课程不存在");
        }
        CourseVO courseVO = new CourseVO();
        BeanUtils.copyProperties(course,courseVO);
        CourseDetail courseDetailSearchVO = new CourseDetail();
        courseDetailSearchVO.setdCourseId(course.getCourseId());
        List<CourseDetail> courseDetails = iCourseDetailService.selectCourseDetailList(courseDetailSearchVO);
        if (CollUtil.isNotEmpty(courseDetails)){
            courseVO.setCourseDetailList(courseDetails);
        }
        return success(courseVO);
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('api:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('api:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('api:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@GetMapping("/delete/{courseId}")
    public AjaxResult remove(@PathVariable Long courseId)
    {
        return toAjax(courseService.deleteCourseByCourseId(courseId));
    }
}
